<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Index extends Controller_Index_Base
{
    public function action_index()
    {           
        $categories = ORM::factory('category')->get_main_categories();
        $categories_tree = ORM::factory('category')->get_categories_tree();
        $models = ORM::factory('model')->get_models_for_main();
        
        $content = new View('index/index/index_tpl');
        $content->categories = $categories;
        $content->categories_tree = $categories_tree;
        $content->models     = $models;    
        
        $this->print_template('Выбор, сравнение и покупка товаров', $content);
        
        //$categories = ORM::factory('category')->get_categories_tree();
        /*$categories = ORM::factory('category')->get_main_categories();  
        
        echo '<pre>';
        print_r($categories);  */
        
        //Request::current()->redirect('/camera');
        /*$models = ORM::factory('model')->get_random_models(30);               
        $content = new View('index/index/index_tpl'); 
        $content->models = $models;       
        $this->print_template('Отзывы о товарах', $content);  */
    }
    
    public function action_search()
    {
        $query = strip_tags($this->request->param('query'));       
        $models = ORM::factory('model')->search($query, 30);
        $content = new View('index/index/search_tpl'); 
        $content->models = $models;       
        $this->print_template('Поиск - '.$query, $content);
    }
    
    public function action_search_ajax()
    {
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $query  = trim(strip_tags($this->request->post('query')));
            if ($query) {
                $models = ORM::factory('model')->search($query, 15);              
                $result = array(
                    'success' => true,
                    'data'    => Helper_Index::models_search_list($models)->render(),
                );
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
    
    public function action_offer()
    {
        if (HTTP_Request::POST == $this->request->method()) {
            $email    = $this->request->post('email');
            //$password = $this->request->post('password');
            $url      = $this->request->post('url');
                  
            try {
                $password = ORM::factory('user')->generate_password();
                
                $user = ORM::factory('user')->add_user($email, $password);
                $shop = ORM::factory('shop')->add_shop($url);                                
                $_POST = array();
                 
                $user->add_to_shop($shop); 
                Model_Auth::instance()->login($email, $password, true);
                  
                $shop->set_as_current();                     
                Request::current()->redirect(Helper_Admin::get_url() . '/index/start'); 
            } catch (Exception $e) {
                $message = 'Форма заполнена с ошибками!';
                if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                    $message = 'Данный e-mail или магазин уже зарегистрированы';
                }
            }
        }
                
        $content = new View('index/index/offer_tpl');        
        $this->response->body($content);
    }
} 
